package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.business.Vaccinator;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.VaccinatorMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IReservationRecordService;
import com.marktotop.service.IVaccinatorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.vo.business.VaccinatorReservationVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class VaccinatorServiceImpl extends ServiceImpl<VaccinatorMapper, Vaccinator> implements IVaccinatorService {

    @Autowired
    private VaccinatorMapper vaccinatorMapper;


    @Autowired
    private IReservationRecordService reservationRecordService;


    /**
     * 接种人员列表(分页)
     *
     * @param pageNum
     * @param pageSize
     * @param vaccinator
     * @return
     */
    @Override
    public ResultBean findVaccinatorList(Integer pageNum, Integer pageSize, Vaccinator vaccinator) {
        // 开启分页
        Page<Vaccinator> page = new Page<>(pageNum,pageSize);
        IPage<Vaccinator> vaccinatorByPage = vaccinatorMapper.getVaccinatorsByPage(page,vaccinator);
        if (vaccinatorByPage.getRecords().isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有接种人员信息成功！")
                .data("total",vaccinatorByPage.getTotal())
                .data("vaccinators",vaccinatorByPage.getRecords());
    }


    /**
     * 编辑接种人员信息
     *
     * @param id
     * @return
     */
    @Override
    public Vaccinator editVaccinator(Long id) throws SystemException {
        Vaccinator vaccinator = vaccinatorMapper.selectById(id);
        if(null == vaccinator){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该编辑的接种人员信息不存在...");
        }
        return vaccinator;
    }


    /**
     * 根据 接种人id查找已预约的疫苗信息
     *
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @Override
    public ResultBean findVaccinatorReservationRecords(Integer pageNum, Integer pageSize, Long id) {
        // 开启分页
        Page<VaccinatorReservationVO> page = new Page<>(pageNum,pageSize);
        IPage<VaccinatorReservationVO> recordsByPage = vaccinatorMapper.getReservationRecordsByPage(page,id);
        if (recordsByPage.getRecords().isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有疫苗预约信息成功！")
                .data("total",recordsByPage.getTotal())
                .data("records",recordsByPage.getRecords());
    }


    /**
     * 查询所有接种人信息，供预约使用
     *
     * @return
     */
    @Override
    public List<Vaccinator> findAllVaccinatorInfo() {
        return vaccinatorMapper.selectList(null);
    }


    /**
     *删除接种人员信息
     *      无预约信息，不删除疫苗历史，疫苗历史会保留部分用户信息
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @Override
    public boolean removeVaccinator(Long id) throws SystemException {
        Vaccinator vaccinator = vaccinatorMapper.selectById(id);
        if (null == vaccinator){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的接种人员信息不存在...");
        }
        // 有预约记录，无法删除
        if (reservationRecordService.haveRecordsByVaccinatorId(vaccinator.getId())){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"当前接种人员存在预约信息，不能删除用户...");
        }
        if ( 0 == vaccinatorMapper.deleteById(id)){
            return false;
        }
        return true;
    }

}
